package com.iculture;


import java.util.ArrayList;
import java.util.Collections;

import com.iculture.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils.TruncateAt;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;

import android.widget.TextView;

public class DataArrayAdapter extends ArrayAdapter<Activity_DataStructure> {

	private Context context;
	ArrayList<Activity_DataStructure> ATObject;
	DisplayImageOptions options;
	ImageLoader imageLoader = ImageLoader.getInstance();

	public DataArrayAdapter(Context context, int textViewResourceId,
			ArrayList<Activity_DataStructure> ATObject) {
		super(context, textViewResourceId, ATObject);
		this.context = context;

		// ImageLoader 初始設定
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.activity_default)
				.showImageForEmptyUri(R.drawable.activity_default)
				.showImageOnFail(R.drawable.activity_default).cacheInMemory()
				.cacheOnDisc().build();

		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				getContext()).defaultDisplayImageOptions(options).build();
		imageLoader.init(config);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
		View rowView = convertView;
		

		final viewHolder holder;
		if (rowView == null) {// 改善效能的寫法(利用holder)
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			rowView = inflater.inflate(R.layout.row_layout, parent, false);

			holder = new viewHolder();
			holder.txt_title = (TextView) rowView.findViewById(R.id.title);
			holder.txt_locationName = (TextView) rowView
					.findViewById(R.id.locationName);
			holder.txt_time = (TextView) rowView
					.findViewById(R.id.time);
			holder.imgView = (ImageView) rowView.findViewById(R.id.image);
			/*holder.button1 = (BgButton) rowView.findViewById(R.id.button1);
			holder.button2 = (BgButton) rowView.findViewById(R.id.button2);
			holder.button3 = (BgButton) rowView.findViewById(R.id.button3);
			holder.button4 = (BgButton) rowView.findViewById(R.id.button4);*/
			rowView.setTag(holder);

		} else {
			holder = (viewHolder) rowView.getTag();
		}

		holder.txt_title.setText(String.valueOf(getItem(position).title));
		holder.txt_title.setEllipsize(TruncateAt.END);
		holder.txt_locationName .setText("活動地點:\n"+String
				.valueOf(getItem(position).locationName));
		holder.txt_locationName.setEllipsize(TruncateAt.END);
		holder.txt_time.setText("活動時間:\n"+getItem(position).time);
		// System.out.println("imgURL: " + getItem(position).imageUrl);
		holder.txt_time.setEllipsize(TruncateAt.END);
		imageLoader.displayImage(getItem(position).imageUrl, holder.imgView,
				options, animateFirstListener);	

		return rowView;
	}

	// 篩掉過長字串(舊版,已不使,改用xml搭配setEllipsize)
	public String textFilter(String content, int number) {
		int pointer = 0, buffer = 0;
		while (buffer <= number && pointer != content.length()) {
			buffer++;
			if (content.charAt(pointer) >= 19968
					&& content.charAt(pointer) < 40623) {
				buffer++;
			}
			pointer++;
		}
		return (pointer == content.length()) ? content.substring(0, pointer)
				: content.substring(0, pointer) + "...";
	}

	static class viewHolder {
		TextView txt_title;
		TextView txt_locationName;
		TextView txt_time;
		ImageView imgView;
		BgButton button1;
		BgButton button2;
		BgButton button3;
		BgButton button4;
	}

	// 圖片顯示動畫
	private static class AnimateFirstDisplayListener extends
			SimpleImageLoadingListener {

		static final java.util.List<String> displayedImages = Collections
				.synchronizedList(new java.util.LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view,
				Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
				} else {
					imageView.setImageBitmap(loadedImage);
				}
				displayedImages.add(imageUri);
			}
		}
	}

}
